(function() {
    
    "use strict";
    
    //===== Prealoder

    window.onload = function() {
        window.setTimeout(fadeout, 5);
    }

    function fadeout() {
        document.querySelector('.preloader').style.opacity = '0';
        document.querySelector('.preloader').style.display = 'none';
    }

    
    /*=====================================
    Sticky
    ======================================= */
    window.onscroll = function () {
        var header_navbar = document.getElementById("header_navbar");
        var logo = document.querySelector("img#logo");
        var sticky = header_navbar.offsetTop;

        if (window.pageYOffset > sticky) {
            header_navbar.classList.add("sticky");
            logo.setAttribute("src", "/assets/images/logo-2.png")
        } else {
            header_navbar.classList.remove("sticky");
            logo.setAttribute("src", "/assets/images/logo.png")
        }



        // show or hide the back-top-top button
        var backToTo = document.querySelector(".back-to-top");
        if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
            backToTo.style.display = "block";
        } else {
            backToTo.style.display = "none";
        }
    };

    // Get the navbar


    // for menu scroll 
    // var pageLink = document.querySelectorAll('.page-scroll');
    //
    // pageLink.forEach(elem => {
    //     elem.addEventListener('click', e => {
    //         e.preventDefault();
    //         document.querySelector(elem.getAttribute('href')).scrollIntoView({
    //             behavior: 'smooth',
    //             offsetTop: 1 - 60,
    //         });
    //     });
    // });

    // section menu active
    function onScroll(event) {
        var sections = document.querySelectorAll('.page-scroll');
        var scrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

        for (var i = 0; i < sections.length; i++) {
            var currLink = sections[i];
            var val = currLink.getAttribute('href');
            var refElement = document.querySelector(val);
            var scrollTopMinus = scrollPos + 73;
            if (refElement.offsetTop <= scrollTopMinus && (refElement.offsetTop + refElement.offsetHeight > scrollTopMinus)) {
                document.querySelector('.page-scroll').classList.remove('active');
                currLink.classList.add('active');
            } else {
                currLink.classList.remove('active');
            }
        }
    };

    // window.document.addEventListener('scroll', onScroll);


    //===== close navbar-collapse when a  clicked
    let navbarToggler = document.querySelector(".navbar-toggler");    
    var navbarCollapse = document.querySelector(".navbar-collapse");

    document.querySelectorAll(".page-scroll").forEach(e =>
        e.addEventListener("click", () => {
            navbarToggler.classList.remove("active");
            navbarCollapse.classList.remove('show')
        })
    );


    //===== glide slider for testimonial
    
    // new Glide('.glide', {
    //     type: 'slider',
    //     perView: 1,
    //     animationDuration: 1000
    // }).mount()
    //
    // //WOW Scroll Spy
    // var wow = new WOW({
    //     //disabled for mobile
    //     mobile: false
    // });
    // wow.init();
    
})();

$(function () {
    $('.yizhu').click(function () {
        $(this).hide();
        $('#'+$(this).attr('g')).show();
        let poemid = $(this).attr('poemid');
        let typeList = [];
        $('.pid_'+poemid).each(function () {
            if($(this).css('display') != 'none' && $(this).attr('extType') != undefined){
                typeList.push($(this).attr('extType'));
            }
        });
        typeList.push('body');
        $.get("/getPoemExt/" + typeList.join('') + "/" + poemid,function(data){
            $('#body_'+poemid).html(data);
        });
    });

    //诗文页面  过滤条件 下拉展开事件
    $('.item-handler').click(function(){
        $(this).parents('.filter-item ').toggleClass('open');
    });
    //作者成就 任务生平 more btn
    $('.more').click(function(){
        $(this).hide();
        $(this).parents('.author-panel ').find('.doc-box').css('height','auto');
        $(this).parents('.author-panel ').find('.doc-box').removeClass('box-mask');
        $(this).parents('.author-panel ').css('height','auto');
    });
});

function selectSearch(){
    var val = $('#searchKey').val();
    if (val==undefined || val===''){
        alert('请输入查询内容！');
        return false;
    }
    return true;
}